(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTerminals();
		});
	});
	
	function initTerminals(){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var studentcode = $(".students-choice-select",parent.document).attr("studentcode");
		var studentname = $(".students-choice-select",parent.document).attr("name");
		if(studentcode != undefined){
			var data = {
					cmd:"getCard",
					model:"card",
					userKey:userKey,
					data:{
						studentcode:studentcode,
					},
					remark:"",					
			};
			AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
				var content = "";
				if(res.result == 1 && res.data != undefined && res.data != ""){
					var data = res.data;
					if(data.length > 0){
						for(var i = 0; i < data.length; i++){
							for(var key in data[i]){
								if(data[i][key] == null || data[i][key] == "null" ){
									data[i][key] = "";
								}
							}
							content += "<div class=\"table-trs\">" +
								"<div class=\"tables-left\">" +
									"<div class=\"table-name\" style=\"width:250px;\" title=\""+studentname+"\"\>"+substr(studentname,8)+"</div>" +
									"<div class=\"table-code\" style=\"width:250px;\" >IMEI： "+data[i].imei+"</div>" +
								"</div>" +
								"<div class=\"tables-middle\">" +
									"<div class=\"table-class\" style=\"width:250px;\">手机： "+data[i].cellphone+"</div>" +
									"<div class=\"table-code\" style=\"width:250px;\">RFID： "+data[i].cardcode+"</div>" +
								"</div>" +
								"<div class=\"tables-right\">" +
									"<div class=\"table-address\" title=\""+data[i].remark+"\" >"+substr(data[i].remark,12)+"</div>" +
									"<div class=\"table-oprate\">" +
										"<div class=\"table-detail\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"详情\"></div>" +
										"<div class=\"table-locked\" imei = \""+data[i].imei+"\"  title=\"挂失\"></div>" +
										"<div class=\"table-account\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"余额\"></div>" +
									"</div>" +
								"</div>" +
							"</div>";
							dataCache.push(data[i]);
						}
						$("#aterminal-data-show").empty();
						$("#aterminal-data-show").append(content);
						
						$(".table-trs").unbind("hover");
						$(".table-trs").hover(function(){
							$(this).find(".table-oprate").show();
						},function(){
							$(".table-oprate").hide();
						});
						
						$(".table-detail").unbind("click");
						$(".table-detail").click(function(){
							$("#aterminal-detailform").show();
							$("#right-aterminal-tables").hide();
							var imei = parseInt($(this).attr("imei"));
							showData(imei);
							$("#aterminal-detailform").contents().find(".add-stu-bottom1").hide();
							$("#aterminal-detailform").contents().find("input").attr("disabled","disabled");
							$("#aterminal-detailform").contents().find("input[type='text']").css("background-color","#E5E5E5");
							$("#aterminal-detailform").contents().find("#back-to-table").unbind("click");
							$("#aterminal-detailform").contents().find("#back-to-table").click(function(){
								$("#aterminal-detailform").hide();
								$("#right-aterminal-tables").show();
							});
						});
						
						$(".table-account").unbind("click");
						$(".table-account").click(function(){
							$("#aterminal-account").show();
							$("#right-aterminal-tables").hide();
							var imei = parseInt($(this).attr("imei"));
							getAccount(imei);
							$("#aterminal-account").contents().find(".add-stu-bottom1").hide();
							$("#aterminal-account").contents().find("input").attr("disabled","disabled");
							$("#aterminal-account").contents().find("input[type='text']").css("background-color","#E5E5E5");
							$("#aterminal-account").contents().find("#back-to-table").unbind("click");
							$("#aterminal-account").contents().find("#back-to-table").click(function(){
								$("#aterminal-account").hide();
								$("#right-aterminal-tables").show();
							});
						});
						$(".table-locked").unbind("click");
						$(".table-locked").click(function(){
							var imei = parseInt($(this).attr("imei"));
							deleteBox("确定要挂失吗？",function(){
								terminalLoss(imei);
							});
						});
						
					}else{
						content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
									"</div>";
						$("#aterminal-data-show").empty();
						$("#aterminal-data-show").append(content);
					}
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#aterminal-data-show").empty();
					$("#aterminal-data-show").append(content);
				}
			},null,null);	
		}else{
			var content = "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
			$("#aterminal-data-show").empty();
			$("#aterminal-data-show").append(content);
		}
		
	}
	function getAccount(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"queryYE",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.data == true){
					$("#aterminal-account").contents().find(".content-account").html("正在查询余额，查询过程可能需要15至20秒，请稍候...");
					getOldAccount(imei);
				}
			}
		},null,null);	
	}
	function getOldAccount(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"queryYE_old",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1){
				if(res.data != ""){
					$("#aterminal-account").contents().find(".content-account").html(res.data + "（如无法点击，请复制链接到地址栏访问并下载）");
				}else{
					setTimeout(function(){
						getOldAccount(imei);
					},5000);
				}
			}
		},null,null);
	}
	function showData(imei){
		getWhiteList(imei);
		getFamilyNumbers(imei);
		getSosNumbers(imei);
		getWorktimes(imei,1);
		getWorktimes(imei,0);
		getAllRings(imei);
	}
	
	function getWhiteList(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getPhonebooks",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#white-number-" + (i+1)) && $("#aterminal-detailform").contents().find("#white-name-" + (i+1))){
						$("#aterminal-detailform").contents().find("#white-number-" + (i+1)).val(list[i].incellphone);
						$("#aterminal-detailform").contents().find("#white-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	function getFamilyNumbers(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getFamilyphones",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#parent-number-" + (i+1))){
						$("#aterminal-detailform").contents().find("#parent-number-" + (i+1)).val(list[i].familyphone);
						$("#aterminal-detailform").contents().find("#parent-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	function getSosNumbers(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getSosphones",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#sos-number-" + (i+1))){
						$("#aterminal-detailform").contents().find("#sos-number-" + (i+1)).val(list[i].sosphone);
						$("#aterminal-detailform").contents().find("#sos-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	
	function getWorktimes(imei,work){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getCardWork",
				model:"card",
				userKey:userKey,
				data:{
					cardcode:imei,
					work:work,
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				if(work == 1){
					for(var i = 1; i < 25; i++){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null" ){
								list[i][key] = "";
							}
						}
						if($("#aterminal-detailform").contents().find("#worktime" + i + "-1") && $("#aterminal-detailform").contents().find("#worktime" + i + "-2")){
							$("#aterminal-detailform").contents().find("#worktime" + i + "-1").val(list["start" + i ]);
							$("#aterminal-detailform").contents().find("#worktime" + i + "-2").val(list["stop" + i ]);
						}
					}
				}else if(work == 0){
					for(var i = 1; i < 25; i++){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null" ){
								list[i][key] = "";
							}
						}
						if($("#aterminal-detailform").contents().find("#stealthtime" + i + "-1") && $("#aterminal-detailform").contents().find("#stealthtime" + i + "-2")){
							$("#aterminal-detailform").contents().find("#stealthtime" + i + "-1").val(list["start"+ i ]);
							$("#aterminal-detailform").contents().find("#stealthtime" + i + "-2").val(list["stop"+ i ]);
						}
					}
				}
			}
		},null,null);	
		
	}
	function getAllRings(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getAllRings",
				model:"card",
				userKey:userKey,
				data:{
					
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				var content = "";
				for(var i = 0; i < list.length; i = i+2){
					content += "<tr><td style=\"width:50%;\"><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i].ring+"' ringurl='"+list[i].ringurl+"' disabled='disabled' /><a href='"+Host+list[i].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i].ring)+"</a></td>";
					content += "<td style=\"width:50%;\"><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i+1].ring+"' ringurl='"+list[i].ringurl+"' disabled='disabled' /><a href='"+Host+list[i+1].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i+1].ring)+"</a></td></tr>";
				}
				$("#aterminal-detailform").contents().find("#table-ring").empty();
				$("#aterminal-detailform").contents().find("#table-ring").append(content);
				getSelectedRing(imei);
				
			}
		},null,null);	
	}
	function getSelectedRing(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getCard",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				//显示已有的铃声设置
				var m = $("#aterminal-detailform").contents().find("#table-ring input");
				for(var i = 0; i < m.length; i++){	
					if(m[i].getAttribute("value") == list[0].ring)
					$("#aterminal-detailform").contents().find("#table-ring input[value="+list[0].ring+"]").attr("checked","checked");
				}
				//显示已有的报警类型设置
				var sostype2 = $("#aterminal-detailform").contents().find("input[name='sostype2']");
				var sostype = list[0].sostype;
				if(sostype == 4 || sostype == "4"){
					$("#aterminal-detailform").contents().find("#sostype-close").attr("checked","checked");
					sostype2.removeAttr("checked");
				}else if(sostype != "" && sostype != "null"){
					$("#aterminal-detailform").contents().find("#sostype-open").attr("checked","checked");
					for(var i = 0; i < sostype2.length; i++){
						if(sostype2[i].getAttribute("value") == sostype)
							$("#aterminal-detailform").contents().find("#sostype-" + sostype).attr("checked","checked");
					}
				}
				for(var i = 0; i < list.length; i++){
					$("#card-model-"+list[i].model).attr("checked","checked");
				}
			}
		},null,null);	
	}
	function terminalLoss(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getTerminalLoss",
				model:"terminal",
				userKey:userKey,
				data:{
					asn:imei,
				},
				remark:""
		};
		processBegin("正在挂失中···");
		AjaxCrud.get(serverUrl.TerminalLossServerLoginUrl,data,function(res){				
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("挂失成功");
				initTerminals(currentPage,pageSize);
			}else{
				processError("挂失失败");
			}
		});
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function isUndefined(param){
		if(param == undefined){
			param = "";
		}
		return param;
	}
	function pagebtns(){
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("students-choice-select");
			$(this).addClass("students-choice-select");
			initTerminals();
		});
		$("#aterminal-name").unbind("keyup");
		$("#aterminal-name").keyup(function(){
			this.value=this.value.replace(/^ +| +$/g,'');
		});
		document.onkeydown = function(e){ 
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	initTerminals(currentPage,pageSize);
		    }
		};
		$("#aterminal-search-btn").click(function(){
			dataCache = [];
			initTerminals(currentPage,pageSize);
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();